﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using RBS.DBUtility;
using RBS.Common;
namespace RBS.Core.DAL
    .Hotel
{
    //酒店用户
    public partial class H_Hotel_Users
    {
        public bool Exists(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from H_Hotel_Users");
            strSql.Append(" where ");
            strSql.Append(" Id = @Id  ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public bool Exists(string email)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from H_Hotel_Users");
            strSql.Append(" where ");
            strSql.Append(" H_UID = @H_UID  ");
            SqlParameter[] parameters = {
					new SqlParameter("@H_UID", SqlDbType.VarChar,50)
			};
            parameters[0].Value = email;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }
        public RBS.Core.Model.Hotel.H_Hotel_Users UserLogin(string hUid, string pwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, HotelId, H_UID, H_UserName, Password, RoleIds, regTime,Status  ");
            strSql.Append("  from H_Hotel_Users ");
            strSql.Append(" where H_UID=@hUid and Password=@Password and Status=0");
            SqlParameter[] parameters = {
					new SqlParameter("@hUid", SqlDbType.VarChar,200),
                    new SqlParameter("@Password", SqlDbType.VarChar,100)
			};
            parameters[0].Value = hUid;
            parameters[1].Value = pwd;

            var model = new RBS.Core.Model.Hotel.H_Hotel_Users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                model.H_UID = ds.Tables[0].Rows[0]["H_UID"].ToString();
                model.H_UserName = ds.Tables[0].Rows[0]["H_UserName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RoleIds = ds.Tables[0].Rows[0]["RoleIds"].ToString();
                if (ds.Tables[0].Rows[0]["regTime"].ToString() != "")
                {
                    model.regTime = DateTime.Parse(ds.Tables[0].Rows[0]["regTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RBS.Core.Model.Hotel.H_Hotel_Users model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into H_Hotel_Users(");
            strSql.Append("HotelId,H_UID,H_UserName,Password,RoleIds,regTime,Status,DeptId");
            strSql.Append(") values (");
            strSql.Append("@HotelId,@H_UID,@H_UserName,@Password,@RoleIds,@regTime,@Status,@DeptId");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@H_UID", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@H_UserName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Password", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@RoleIds", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@regTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@DeptId", SqlDbType.NVarChar,50)             
              
            };

            parameters[0].Value = model.HotelId;
            parameters[1].Value = model.H_UID;
            parameters[2].Value = model.H_UserName;
            parameters[3].Value = model.Password;
            parameters[4].Value = model.RoleIds;
            parameters[5].Value = model.regTime;
            parameters[6].Value = model.Status;
            parameters[7].Value = model.DeptId;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBS.Core.Model.Hotel.H_Hotel_Users model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update H_Hotel_Users set ");

            strSql.Append(" HotelId = @HotelId , ");
            strSql.Append(" H_UID = @H_UID , ");
            strSql.Append(" H_UserName = @H_UserName , ");
            strSql.Append(" Password = @Password , ");
            strSql.Append(" RoleIds = @RoleIds , ");
            strSql.Append(" regTime = @regTime , ");
            strSql.Append(" Status = @Status , ");
            strSql.Append(" DeptId = @DeptId  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4) ,            
                        new SqlParameter("@H_UID", SqlDbType.NVarChar,200) ,            
                        new SqlParameter("@H_UserName", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Password", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@RoleIds", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@regTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@DeptId", SqlDbType.NVarChar,50)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.HotelId;
            parameters[2].Value = model.H_UID;
            parameters[3].Value = model.H_UserName;
            parameters[4].Value = model.Password;
            parameters[5].Value = model.RoleIds;
            parameters[6].Value = model.regTime;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.DeptId;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("delete from H_Hotel_Users ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from H_Hotel_Users ");
            strSql.Append(" where ID in (" + Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBS.Core.Model.Hotel.H_Hotel_Users GetModel(int Id)
        {

            var strSql = new StringBuilder();
            strSql.Append("select Id, HotelId, H_UID, H_UserName, Password, RoleIds, regTime, Status, DeptId  ");
            strSql.Append("  from H_Hotel_Users ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;


            RBS.Core.Model.Hotel.H_Hotel_Users model = new RBS.Core.Model.Hotel.H_Hotel_Users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                model.H_UID = ds.Tables[0].Rows[0]["H_UID"].ToString();
                model.H_UserName = ds.Tables[0].Rows[0]["H_UserName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RoleIds = ds.Tables[0].Rows[0]["RoleIds"].ToString();
                if (ds.Tables[0].Rows[0]["regTime"].ToString() != "")
                {
                    model.regTime = DateTime.Parse(ds.Tables[0].Rows[0]["regTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                model.DeptId = ds.Tables[0].Rows[0]["DeptId"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }

        public RBS.Core.Model.Hotel.H_Hotel_Users GetModel(string hUId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, HotelId, H_UID, H_UserName, Password, RoleIds, regTime,Status  ");
            strSql.Append("  from H_Hotel_Users ");
            strSql.Append(" where H_UID=@hUId");
            SqlParameter[] parameters = {
					new SqlParameter("@hUId", SqlDbType.VarChar,200)
			};
            parameters[0].Value = hUId;

            var model = new RBS.Core.Model.Hotel.H_Hotel_Users();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["HotelId"].ToString() != "")
                {
                    model.HotelId = int.Parse(ds.Tables[0].Rows[0]["HotelId"].ToString());
                }
                model.H_UID = ds.Tables[0].Rows[0]["H_UID"].ToString();
                model.H_UserName = ds.Tables[0].Rows[0]["H_UserName"].ToString();
                model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
                model.RoleIds = ds.Tables[0].Rows[0]["RoleIds"].ToString();
                if (ds.Tables[0].Rows[0]["regTime"].ToString() != "")
                {
                    model.regTime = DateTime.Parse(ds.Tables[0].Rows[0]["regTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Status"].ToString() != "")
                {
                    model.Status = int.Parse(ds.Tables[0].Rows[0]["Status"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM H_Hotel_Users ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM H_Hotel_Users ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * FROM H_Hotel_Users ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where 1=1 " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion  ExtensionMethod
        /// <summary>
        /// 用户第一次修改酒店ID
        /// </summary>
        /// <param name="hotleId"></param>
        /// <param name="hUserId"></param>
        public bool UpdateHotelId(int hotleId, int hUserId)
        {
            const string strSql = "update H_Hotel_Users set HotelId=@HotelId where id=@id";
            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@HotelId", SqlDbType.Int,4)    
            };
            parameters[0].Value = hUserId;
            parameters[1].Value = hotleId;
            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }



        }


        public bool UpdateStatus(int hUserId, int status)
        {
            const string strSql = "update H_Hotel_Users set [Status]=@Status where Id=@Id";
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
                    new SqlParameter("@Status", SqlDbType.Int,4)
			};
            parameters[0].Value = hUserId;
            parameters[1].Value = status;
            int rows = DbHelperSQL.ExecuteSql(strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}

